﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Kristall.Engine;

namespace Kristall
{
    public class Projectile : Animatable
    {
        public float Damage { get; set; }
        public float Speed { get; set; }
        public Vector2 Direction { get; set; }
        public bool IsAlive { get; set; }
        public float LifeSpan { get; set; }

        public virtual string ImpactSoundCue
        {
            get
            {
                return "Hit";
            }
        }

        float _timer = 0;

        public AABB Host;

        public Projectile(AABB host = null)
        {
            Host = host;

            CheckPlayerCollision = true;
            CheckWorldCollision = false;

            LifeSpan = 1.0f;
            IsAlive = true;
            Speed = 1.0f;
            Direction = Vector2.UnitX;
            Damage = 0.1f;
        }

        public void CollisionWithActor(Actor actor)
        {
            if (!IsAlive || !actor.IsAlive)
                return;

            Kristall.SoundManager.PlayNonInstancedSound(ImpactSoundCue);
            actor.Health -= this.Damage;
            this.IsAlive = false;
        }

        public override void Update(float dt)
        {
            _timer += dt;

            if (_timer > LifeSpan)
            {
                IsAlive = false;
            }

            if (!IsAlive)
            {
                Level.PlayerProjectiles.Remove(this);
                Level.EnemyProjectiles.Remove(this);
            }

            Velocity = Speed * Direction;
            this.Position += Velocity * dt;

            base.Update(dt);
        }

    }
}
